﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Runda.CMS.Common.Entity;
using System.Collections.Generic;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Dal
{
    public class CommonDal:DalBase
    {
        public DataTable getSqlData(string sql)
        {            
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);           
            return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }

        public SqlDataReader getSqlData_reader(string sql)
        {
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            
            using (IDataReader reader = CurrentDatabase.ExecuteReader(dbcommand))
            {
                return this.GetFromReader<SqlDataReader>(reader);
            }
            
        }

        public DataSet getSqlData_DS(string sql)
        {
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            dbcommand.CommandTimeout = 0;
            return CurrentDatabase.ExecuteDataSet(dbcommand);
        }

        public SysAutoCodeRuleEntity GetEntity(string CodeRuleName)
        {
            SysAutoCodeRuleEntity entity = null;
            DbCommand dbcommand = CurrentDatabase.GetStoredProcCommand("proc_GenId");
            CurrentDatabase.AddInParameter(dbcommand, "@RuleId", DbType.String, CodeRuleName);
            using (IDataReader reader = CurrentDatabase.ExecuteReader(dbcommand))
            {
                entity = this.GetFromReader<SysAutoCodeRuleEntity>(reader);
            }
            return entity;
        }

        public int DropTable(string tblName)
        {
            if (tblName == "") { return 0; }
            string sql = "drop table " + tblName;

            DbCommand dbCommand = this._db.GetSqlStringCommand(sql);
            int irtn = CurrentDatabase.ExecuteNonQuery(dbCommand);
            return irtn;
        }
    }
}
